package library.common.util;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.UUID;

/**
 * @author Created by qiang.hou on 2018/12/6.
 * @version 1.0
 */
public class DeviceUtils {

    /**
     * 获取设备唯一标识符
     *
     * @param context Context
     * @return device id
     */
    public static String getDeviceCode(Context context) {
        String deviceId = "";
        try {
            // device id
            deviceId = getDeviceId(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (deviceId != null && !deviceId.equals("")) {
            return deviceId;
        }

        try {
            // mac address 去掉中间相隔的冒号
            deviceId = getLocalMac(context).replace(":", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (deviceId != null && !deviceId.equals("")) {
            return deviceId;
        }

        // uuid
        UUID uuid = UUID.randomUUID();
        deviceId = uuid.toString().replace("-", "");

        return deviceId;
    }


    /**
     * 获取设备号码
     *
     * @param context Context
     * @return device id
     */
    @SuppressLint("HardwareIds")
    private static String getDeviceId(Context context) {
        try {
            TelephonyManager service = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                return service.getDeviceId();
            }
            return "";
        } catch (Exception e) {
            return ""; //获取不到设备信息
        }
    }

    /**
     * get mac address
     * 由于 6.0 以后 WifiManager 得到的 MacAddress得到都是 相同的没有意义的内容,所以采用以下方法获取Mac地址
     *
     * @param context Context
     * @return mac address
     */
    private static String getLocalMac(Context context) {
        String macAddress = null;
        StringBuilder buf = new StringBuilder();
        NetworkInterface networkInterface = null;
        try {
            networkInterface = NetworkInterface.getByName("eth1");
            if (networkInterface == null) {
                networkInterface = NetworkInterface.getByName("wlan0");
            }
            if (networkInterface == null) {
                return "";
            }
            byte[] address = networkInterface.getHardwareAddress();

            for (byte b : address) {
                buf.append(String.format("%02X:", b));
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            macAddress = buf.toString();
        } catch (SocketException e) {
            e.printStackTrace();
            return "";
        }
        return macAddress;
    }

    public enum DeviceType {
        DEVICE_ID,
        DEVICE_SOFTWARE_VERSION,
        LINE1_NUMBER,
        NETWORK_COUNTRY_ISO,
        NETWORK_OPERATOR,
        NETWORK_OPERATOR_NAME,
        NETWORK_TYPE,
        PHONE_TYPE,
        SIM_COUNTRY_ISO,
        SIM_OPERATOR,
        SIM_OPERATOR_NAME,
        SIM_SERIALNUMBER,
        SIM_STATE,
        SUBSCRIBER_ID,
        VOICE_MAIL_NUMBER,
        CALL_STATE,
        DATA_ACTIVITY,
        DATA_STATE,
        VOICE_MAIL_ALPHA_TAG,
        HAS_ICC_CARD,
        IS_NETWORK_ROAMING
    }
}